﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eCommerce.DataAccess;

namespace eCommerce.CMS.CustomAdmin
{
    public partial class Subscriber : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string actionType = this.Request.QueryString["action"] == null ? "List" : Convert.ToString(this.Request.QueryString["action"]);
                string pageID = this.Request.QueryString["PageID"] == null ? string.Empty : Convert.ToString(this.Request.QueryString["PageID"]);
                string CategoryTitle = string.Empty;
                string titlebase = "Page";
                Int32 Pagid;
                if (Int32.TryParse(Request.QueryString["PageID"], out Pagid))
                {
                    using (WebsiteDataContext adatabaseDataContext = new WebsiteDataContext())
                    {
                        {
                            var pag = (from x in adatabaseDataContext.Xcms_Subscribers
                                       where x.id == Pagid
                                       select x).Take(1).SingleOrDefault();
                            if (pag != null)
                            {
                                titlebase = pag.name.Trim();
                                ContentPanelLbl.Text = "<h3>" + titlebase + "</h3>";
                            }

                        }
                    }
                }

                switch (actionType)
                {
                    case "Add":
                        CategoryTitle = "<h3>Add " + titlebase + "</h3>";
                        Add1.Visible = true;
                        Edit1.Visible = false;
                        break;
                    case "Edit":
                        CategoryTitle = "<h3>Edit - " + titlebase + "</h3>";
                        Add1.Visible = false;
                        Edit1.Visible = true;
                        break;
                    case "List":
                        CategoryTitle = "<h3>List of " + titlebase + "s</h3>";
                        List1.Visible = true;
                        break;
                    case "Delete":
                        CategoryTitle = "<h3>Delete - " + titlebase + "s</h3>";
                        List1.Visible = true;
                        break;
                    default:
                        CategoryTitle = "<h3>List of" + titlebase + "s</h3>";
                        List1.Visible = true;
                        break;
                }

                if (actionType == "Edit" && pageID == string.Empty)
                {
                    ContentPanelLbl.Text = "<h3>Subscriber ID has not been assigned in edit mode</h3>";
                }
                else
                {
                    if (actionType == "Edit")
                    {
                        //Edit1.PageID = PageID;
                        ContentPanelLbl.Text = "<h3>Edit - " + titlebase + "</h3>";
                    }
                    else if (actionType == "Add")
                    {
                        ContentPanelLbl.Text = "<h3>Add New Subscriber</h3>";
                    }
                    else if (actionType == "Delete")
                    {
                        DeletePage(pageID);
                    }
                }
            }
        }
        public void DeletePage(string pageID)
        {
            using (WebsiteDataContext ndc = new WebsiteDataContext())
            {
                try
                {
                    var oHarrierPage = (from p in ndc.Xcms_Subscribers
                                        where p.id == Convert.ToInt32(pageID)
                                        select p).First();

                    string record = "'" + oHarrierPage.name + "' has been deleted";

                    ndc.Xcms_Subscribers.DeleteOnSubmit(oHarrierPage);
                    ndc.SubmitChanges();
                    PanelStatusMsg.Text = record;

                }
                catch (Exception ex)
                {
                    PanelStatusMsg.Text = "An error has occured. Message: " + ex.Message;

                }

            }
        }
    }
}
